﻿using Microsoft.AspNetCore.Mvc.ModelBinding;
using System.Collections.Generic;
using System.Linq;

namespace Cxuu.Common
{
    public static class ModelStateDictionaryExtensions
    {
        /// <summary>
        /// 获取ModelStateDictionary中包含的所有错误信息
        /// </summary>
        /// <param name="modelStateDictionary"></param>
        /// <returns></returns>
        public static string GetErrorMessages(this ModelStateDictionary modelStateDictionary)
        {
            string messages = "";
            List<string> Keys = modelStateDictionary.Keys.ToList();
            //获取每一个key对应的ModelStateDictionary
            foreach (var key in Keys)
            {
                var errors = modelStateDictionary[key].Errors.ToList();
                //将错误描述添加到sb中
                foreach (var error in errors)
                {
                    messages += error.ErrorMessage + " \r\n";
                }
            }
            return messages;
        }
    }
}
